<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>客户体检信息健康管理系统</title>
    <meta name="description" content="客户体检信息健康管理系统">
    <meta name="keywords" content="客户体检信息健康管理系统">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
</head>
<body>
<div id="app">
    <div class="content-header">
        <h1>健康咨讯<small>健康咨讯发布</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>健康咨讯</el-breadcrumb-item>
            <el-breadcrumb-item>健康咨讯发布</el-breadcrumb-item>
        </el-breadcrumb>
        <div class="app-container">
            <div class="box">
                <!-- 新增标签弹层 -->
                <el-form ref="dataAddForm" :model="information" :rules="rules" label-position="right"
                         label-width="100px">
                    <el-row>
                        <el-col>
                            <el-form-item label="标题" prop="title">
                                <el-input v-model="information.title"/>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="12"
                                v-if="user.roles!=null && user.roles[0].keyword=='ROLE_ADMIN'">
                            <el-form-item label="健康管理师" prop="user.id">
                                <el-select v-model="information.user.id">
                                    <el-option v-for="(user,index) in userList" :key="user.id" :label="user.username"
                                               :value="user.id"></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                       <!-- <el-col>
                            <el-form-item label="发布时间" prop="releaseTime">
                                <el-date-picker
                                        v-model="information.releaseTime"
                                        type="date"
                                        placeholder="选择日期">
                                </el-date-picker>
                            </el-form-item>
                        </el-col>-->
                    </el-row>
                    <el-row>
                        <el-col :span="24">
                            <el-form-item label="内容" prop="content">
                                <el-input v-model="information.content" type="textarea"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>
                <div style="width: 100%;margin: auto 0; text-align:center;" class="dialog-footer">
                    <el-button type="primary" @click="submitForm('dataAddForm')">提交</el-button>
                    <el-button @click="resetForm('dataAddForm')">重置</el-button>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index_copy.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script>
    var vue = new Vue({
        el: '#app',
        data: {
            user: {},
            information: {
                user: {
                    id: ''
                }
            },
            userList: [],
            rules: {//校验规则
                title: [{required: true, message: '标题为必填项', trigger: 'blur'}],
                content: [{required: true, message: '内容为必填项', trigger: 'blur'}],
                // releaseTime: [{required: true, message: '发布时间为必选项', trigger: 'change'}],
                "user.id": [{required: true, message: '健康管理师为必选项', trigger: 'change'}]
            }
        },
        //钩子函数，VUE对象初始化完成后自动执行
        mounted() {
            //根据用户名查询当前登录的用户
            this.findUser();
        },
        methods: {
            //日期转字符串格式
            formatDate(dateStr) {
                if (dateStr != null) {
                    let newdateStr = dateStr.replace(/\d+(?=-[^-]+$)/, (a) => (parseInt(a, 10) - 1));  //字符串处理(月份)
                    let nums = newdateStr.match(/\d+/g);  //取数
                    let date = eval('new Date(' + nums + ')');  //转换
                    //重组
                    let datetime =
                        date.getFullYear() + "年" +  //年
                        ((date.getMonth() + 1) > 9 ? (date.getMonth() + 1) : "0" + (date.getMonth() + 1)) + "月" +  //月
                        (date.getDate() > 9 ? date.getDate() : ("0" + date.getDate())) + "日";  //日
                    return datetime;
                }
            },
            async findUser() {
                this.user = JSON.parse(localStorage.getItem("userInfo"))
                if (this.user.roles[0].keyword != 'ROLE_ADMIN') {
                    this.information.user.id = this.user.id
                }
                //查询所有健康管理师
                let result = await axios.get('/user/findUserRoleAll.do');
                this.userList = result.data.data
            },
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        //发送添加请求
                        axios.post('/information/addInformation.do', this.information).then((result) => {
                            if (result.data.flag) {
                                this.$message.success(result.data.message)
                                //清空表单
                                this.information = {user: {id: ""}};
                                this.$refs[formName].resetFields();
                                //跳转健康资讯页面
                                let elementsByClassName = window.parent.document.getElementsByClassName('el-menu-item is-active')[0];
                                elementsByClassName.nextElementSibling.click()
                                window.location.href = 'information.html';
                            } else {
                                this.$message.error(result.data.message)
                            }
                        }).catch((e) => {

                        })
                    } else {
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        },


    })
</script>
</html>